'use strict';
// Copyright 2014 Blueant Inc. All Rights Reserved.

/**
 * @fileoverview 支付
 * @author sundbcn@gmail.com (sun debing)
 */

var _ = require('lodash');
var moment = require('moment');
var mongoose = require('mongoose');
var autoIncrement = require('mongoose-auto-increment');
var payMgr = require('../mgr/payMgr');
var payConf = require('../config/payConf');

var _initNextSinglePay = function () {
    return payConf.getNextSinglePayConf(0).id;
};

/**
 * 支付
 */
var paySchema = new mongoose.Schema({
    // 角色id
    role_id: {
        type: Number,
        required: true
    },

    // 已经充值的商品列表
    pay_product_list: [Number],

    // 月卡结束时间
    month_end_tm: {
        type: Number,
        default: 0
    },

    // 至尊卡(终生卡)额度
    persistent_val: {
        type: Number,
        default: 0
    },

    // 下一档单笔充值额度
    next_single_pay: {
        type: Number,
        default: _initNextSinglePay
    }
});

paySchema.pre('save', function (next) {
    this.increment();
    if (!this.isNew && !!this.isModified()) {
        this.pushModify();
    }
    next();
});

/**
 * 是否为至尊卡用户
 * @param {[type]} add_day [description]
 */
paySchema.methods.isExtreme = function () {
    if (this.persistent_val >= 0) {
        return true;
    } else {
        return false;
    }
};

/**
 * 添加月卡的天数
 * @param {[type]} add_day [description]
 */
paySchema.methods.addMonthDay = function (add_day) {
    if (add_day <= 0) {
        return;
    }

    var now_tm = moment().unix();
    if (this.month_end_tm < now_tm) {
        this.month_end_tm = moment.unix(now_tm).startOf('day').unix() - 1;
    }
    this.month_end_tm += add_day * 86400;
};

/**
 * 是否在月卡有效期内
 * @param {[type]} add_day [description]
 */
paySchema.methods.isMonthDay = function () {
    return moment().unix() < this.month_end_tm;
};

/**
 * 设置终身卡额度
 * @param {[type]} pay_val [description]
 */
paySchema.methods.setPersistentVal = function (persistent_val) {
    this.persistent_val = persistent_val;
};

/**
 * 获取终生卡额度
 * @param {[type]} add_day [description]
 */
paySchema.methods.getPersistentVal = function () {
    return this.persistent_val;
};

/**
 * 添加商品列表
 * @param {[type]} product_id [description]
 */
paySchema.methods.addProductId = function (product_id) {
    product_id = +product_id;
    this.pay_product_list.push(product_id);
    this.pay_product_list = _.uniq(this.pay_product_list);
};

/**
 * 是否购买过该商品
 * @param  {[type]}  product_id [description]
 * @return {Boolean}            [description]
 */
paySchema.methods.inPayList = function (product_id) {
    product_id = +product_id;
    return (_.indexOf(this.pay_product_list, product_id) !== -1);
};

/**
 * 添加到限购商品列表
 * @param {[type]} product_id [description]
 */
paySchema.methods.addToLimitPayList = function (product_id) {
    product_id = +product_id;
    this.single_pay_list.push(product_id);
    this.single_pay_list = _.uniq(this.single_pay_list);
};

/**
 * 是否购买过限购商品
 * @param  {[type]}  product_id [description]
 * @return {Boolean}            [description]
 */
paySchema.methods.inLimitPayList = function (product_id) {
    product_id = +product_id;
    return (_.indexOf(this.single_pay_list, product_id) !== -1);
};

/**
 * 获取修改
 * @return {[type]} [description]
 */
paySchema.methods.getLastChange = function () {
    return this.toJSON();
};

/**
 * 推送给用户
 * @return {[type]} [description]
 */
paySchema.methods.push = function () {
    this.pushModify();
};

paySchema.methods.pushModify = function () {
    payMgr.sendPayChanged(
        this.role_id,
        this.getLastChange(),
        function (err, fails) {
            if (err) {
                console.error('send pay message error: %j, fail ids: %j', err, fails);
                return;
            }
        }
    );
};

if (!paySchema.options.toJSON) {
    paySchema.options.toJSON = {};
}
/* jshint unused:false */
paySchema.options.toJSON.transform = function (doc, ret) {
    ret.month_leave_day = _getMonthCardLeaveDay(ret.month_end_tm);
    delete ret._id;
    delete ret.__v;
};

// 计算月卡剩余天数
var _getMonthCardLeaveDay = function (month_end_tm) {
    var today = moment().startOf('day').unix();
    var month_leave_day = (month_end_tm - today + 1) / 86400;
    if (month_leave_day < 0) {
        month_leave_day = 0;
    }

    return month_leave_day;
};

mongoose.model('Pay', paySchema);
